package com.lianyu507.service.impl;

import com.lianyu507.mapper.CrArticleMapper;
import com.lianyu507.mapper.CrWordTranslateMapper;
import com.lianyu507.pojo.CrArticle;
import com.lianyu507.pojo.CrWordTranslate;
import com.lianyu507.pojo.CrWordTranslateExample;
import com.lianyu507.service.TranslateService;
import com.lianyu507.tools.TokenizerTool;
import com.lianyu507.tools.TranslateTool;

import com.lianyu507.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/21.
 * <p>
 * 描述:
 */


@Service
public class TranslateServiceImpl implements TranslateService {
    @Autowired
    private CrArticleMapper crArticleMapper;
    @Autowired
    private CrWordTranslateMapper crWordTranslateMapper;

    @Override
    public JSONResult translate(Integer articleId, String english) {
        JSONResult result=new JSONResult();
        CrArticle crArticle = crArticleMapper.selectByPrimaryKey(articleId);
        
        if (crArticle != null) {
            String pictureDes = crArticle.getPictureDes();
            String pictureTitle = crArticle.getPictureTitle();
            String title = crArticle.getTitle();
            List<String> list = new ArrayList<String>();
            list.add(pictureDes);
            list.add(title);
            list.add(pictureTitle);
            List<String> tokenizer = new ArrayList<String>();
            for (String item : list) {
                try {
                    tokenizer = TokenizerTool.tokenizer(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                tokenizer = TokenizerTool.tokenizer(item);
                for (String word : tokenizer) {
                    CrWordTranslateExample example = new CrWordTranslateExample();
                    example.createCriteria().andEnglishEqualTo(word);
                    List<CrWordTranslate> words = crWordTranslateMapper.selectByExample(example);
                    //如果为空，说明该单词在数据库中不存在
                    if (words.isEmpty()) {
                        CrWordTranslate crWordTranslate = new CrWordTranslate();
                        crWordTranslate.setEnglish(word);
                        String chineseWord = TranslateTool.convertYoudaoSigleWord(word);
                        crWordTranslate.setStatus((byte) 1);
                        crWordTranslate.setCreateTime(new Date());
                        crWordTranslate.setUpdateTime(new Date());
                        crWordTranslate.setChinese(chineseWord);
                        crWordTranslateMapper.insert(crWordTranslate);
                        result.setSuccess(true);
                        result.setMsg("insert transted success");
                    }
                    //如果该list的长度大于1，说明数据库中有重复的单词
                    if (words.size() > 1) {
                        result.setSuccess(false);
                        result.setMsg("exits already");
                        return result;
                    }
                }
                return result;
            }
            
        }
        return null;
    }
}
